from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.models import User
from django.template import RequestContext
from django.shortcuts import render_to_response
from qbank.forms import AuthorForm
from qbank.models import quesAns
from qbank.models import dialog
import csv

@csrf_exempt
def authorView(request):
	if request.method == 'POST':
		form = AuthorForm(request.POST, request.FILES)
		if form.is_valid():
			file = request.FILES['file']
			reader = csv.reader(file, delimiter=',', quotechar='"')
			for row in reader:
				dName = row[0]
				ques = row[1]
				ans = row[2]
				aRate = row[3]
				d, created = dialog.objects.get_or_create(dialogName=dName)
				quesAns.objects.create(dialog=d, question=ques, answer=ans,authorRating=aRate)
			return HttpResponseRedirect('/admin/qbank/dialog/')	
	else:
		form = AuthorForm()
	variables = RequestContext(request, {'form':form})
	return render_to_response('author.html', variables)
	
